#!/bin/bash

exitRC=0
logFile=/tmp/hmc_install.log

#-------------------------------------------------------------------------------
# Common exit point
#-------------------------------------------------------------------------------
exit_cleanup() {

   # keep the log file, but ensure a different user can overwrite it next time
   chmod 666 $logFile

   exit $1$exitRC
}

#-------------------------------------------------------------------------------
# RPM  install
#-------------------------------------------------------------------------------
dorpm() {

   if [ -f /opt/hsc/data/config/NO_UPDATE_RPMS ]
   then
     x="$2"
# Strip version then leading directory name
     f=`echo ${x%%-[0-9]*}`
     r=`echo ${f##*/}`
     for i in `cat /opt/hsc/data/config/NO_UPDATE_RPMS`
     do
       if [ "$r" = "$i" ]
       then
          return
       fi
     done
   fi
     
   # Log the test install output. The format for the "normal" RPM install
   # processing inside this script is 'rpm -i <file spec> --force --nodeps'
   rpm $* 2>> $logFile
   RC=$?
   return $RC
}

#-------------------------------------------------------------------------------
# Start the product install...
#-------------------------------------------------------------------------------
cd /
image=$1

if [ "$image" = "" ]
then
   echo "Please specify directory containing installable packages"
   echo "usage: installImages  <directory>"
   exit_cleanup 1
fi

# check if directory exists
if [ ! -d $image ]
then
 echo "The directory $patchdir doesn't exist"
 echo "Please specify directory containing the installable packages."
 exit_cleanup 2
fi

if [ -f /opt/hsc/data/config/NO_UPDATE_FILES ]
then
   rm -f /tmp/saved_files.tar
   cat /opt/hsc/data/config/NO_UPDATE_FILES | xargs tar -cvf /tmp/saved_files.tar
fi

PATH=$PATH:/opt/IBMJava/jre/bin:
LD_LIBRARY_PATH=/opt/hsc/lib:/opt/hsc/lib/hcmjni:/lib:/usr/lib:$LD_LIBRARY_PATH
export PATH LD_LIBRARY_PATH

LANG=en_US
export LANG
echo "--- Installing MH00157 -----"
grep -q "startHDWR_SVR" /etc/inittab
if [ $? -ne 0 ]
then
    echo "hsv:2345:respawn:/opt/hsc/bin/startHDWR_SVR 1>/dev/null 2>&1" >>/etc/inittab
fi
grep -v "^ss:2345" /etc/inittab > /tmp/_inittab_saved
mv /tmp/_inittab_saved /etc/inittab

grep -q "MH00176: Hardware Server startup Fix (11-10-2004)" /opt/hsc/data/version
if [ $? -ne 0 ]
then
  echo "MH00176: Hardware Server startup Fix (11-10-2004)" >> /opt/hsc/data/version
fi
exit_cleanup 0
